package model

import (
	"gorm.io/gorm"
	"micro-chat/config"
	"time"
)

type Moments struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	UserId    int32          `gorm:"column:user_id;type:int;comment:发布者ID;not null;" json:"user_id"` // 发布者ID
	Content   string         `gorm:"column:content;type:varchar(100);comment:文字内容;not null;" json:"content"`
	Location  string         `gorm:"column:location;type:varchar(50);comment:发布地点;default:NULL;" json:"location"`                              // 发布地点
	LikeCount int32          `gorm:"column:like_count;type:int;comment:点赞数;not null;" json:"like_count"`                                       // 点赞数
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);comment:点赞时间;not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 点赞时间
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);comment:更新时间;not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"` // 更新时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

type MomentsLike struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	MomentsId int32          `gorm:"column:moments_id;type:int;comment:朋友圈ID;not null;" json:"moments_id"`                                     // 朋友圈ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`                                            // 用户ID
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);comment:点赞时间;not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 点赞时间
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);comment:更新时间;not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"` // 更新时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (m *Moments) TableName() string {
	return "moments"
}

func (l *MomentsLike) TableName() string {
	return "moments_list"
}

// 发布朋友圈
func (m *Moments) AddMoment() error {
	return config.DB.Create(&m).Error
}

// 查看
func (m *Moments) FindMoment() (list []*Moments, err error) {
	err = config.DB.Order("created_at desc").Find(&list).Error
	return
}

// 点赞
func (l *MomentsLike) AddLike() error {
	return config.DB.Create(&l).Error
}
